from flask_restful import Resource, marshal_with
from App.model import *
from App.util.response import ResponseUtils
from flask import request
from App.exception import RequestParamException


class UserResource(Resource):
    @marshal_with(build_response_type(user_vo))
    def get(self, id):
        user = User.query.get(id)
        if user is None:
            raise RequestParamException(msg='用户不存在')
        return ResponseUtils.success(user)

    @marshal_with(build_response_type())
    def post(self):
        param = request.get_json()
        user = User(**param)
        user.id = None
        try:
            with db.session.begin():
                db.session.add(user)
        except Exception as e:
            db.session.rollback()
            return ResponseUtils.fail(500, str(e))
        return ResponseUtils.success()
